﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace System.Data.DataSetGenerator
{
    /// <summary>
    /// Represents a collection of the ColumnDescriptor objects
    /// </summary>
    public sealed class ColumnCollection : Collection<ColumnDescriptor>
    {

        /// <summary>
        /// Creates a new ColumnDescriptor object by using the  
        /// giving name and adds it to the collection.
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public ColumnDescriptor Add(String columnName)
        {
            if (String.IsNullOrEmpty(columnName))
                throw new ArgumentNullException("columnName");

            ColumnDescriptor result = new ColumnDescriptor();
            result.ColumnName = result.Caption = columnName;
            Add(result);
            return result;
        }

        /// <summary>
        /// Creates an array of the ColumnDescriptor elements and adds them to the end of the collection.
        /// </summary>
        /// <param name="columnNames"></param>
        public void AddRange(IEnumerable<String> columnNames)
        {
            if (columnNames == null)
                throw new ArgumentNullException("columnNames");
            foreach (var columnName in columnNames)
                Add(columnName);
        }

        /// <summary>
        /// Gets a specific element of the collection by name.
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public ColumnDescriptor this[String columnName]
        {
            get
            {
                if (columnName == null)
                    throw new ArgumentNullException("columnName");

                foreach (var col in this)
                {
                    if (columnName.Equals(col.ColumnName))
                        return col;
                }

                return null;
            }
        }

        /// <summary>
        /// Returns a value indicating whether a collection contains a ColumnDescriptor
        /// element with a given name.
        /// </summary>
        /// <param name="columnName">The ColumnDescriptor's name</param>
        /// <returns></returns>
        public bool Contains(String columnName)
        {
            var col = this[columnName];
            return col != null;
        }
    }
}
